using Microsoft.EntityFrameworkCore;
using System;
using Terra.Core.Common.Utils;
using Terra.Data.Entities.Identity;

namespace Terra.Data.SeedData.IdentitySeedData;

/// <summary>
/// 用户种子数据提供者
/// </summary>
public class UserSeedData : ISeedDataProvider
{
    /// <summary>
    /// 系统内置用户ID常量
    /// </summary>
    public static class SystemUserIds
    {
        /// <summary>
        /// 管理员用户ID
        /// </summary>
        public static readonly Guid AdminUserId = Guid.Parse("308A6931-2E89-4432-B71C-478F6414A127");

        /// <summary>
        /// 系统用户ID
        /// </summary>
        public static readonly Guid SystemUserId = Guid.Parse("F5D39BCE-5C6B-4B98-976D-D2787D9F2240");
    }

    /// <summary>
    /// 配置用户种子数据
    /// </summary>
    /// <param name="modelBuilder">模型构建器</param>
    public void Configure(ModelBuilder modelBuilder)
    {
        var now = DateTime.UtcNow;
        var adminId = SystemUserIds.AdminUserId;

        // 生成密码哈希和盐（这里使用默认密码 "Admin@123"）
        var passwordSalt = SecurityUtils.GenerateSalt();
        var passwordHash = SecurityUtils.HashPassword("Admin@123", passwordSalt);

        // 管理员用户
        modelBuilder.Entity<User>().HasData(new User
        {
            Id = adminId,
            UserName = "admin",
            NormalizedUserName = "ADMIN",
            Email = "admin@terra.com",
            NormalizedEmail = "ADMIN@TERRA.COM",
            EmailConfirmed = true,
            PasswordHash = passwordHash,
            PasswordSalt = passwordSalt,
            SecurityStamp = Guid.NewGuid().ToString(),
            ConcurrencyStamp = Guid.NewGuid().ToString(),
            PhoneNumber = null,
            PhoneNumberConfirmed = false,
            TwoFactorEnabled = false,
            LockoutEnd = null,
            LockoutEnabled = true,
            AccessFailedCount = 0,
            CreatedTime = now,
            CreatedBy = adminId,
            LastModifiedTime = now,
            LastModifiedBy = adminId,
            IsDeleted = false,
            DeletedTime = null,
            DeletedBy = null
        });
    }

    /// <summary>
    /// 获取提供者优先级（先配置用户）
    /// </summary>
    public int Order => 10;
}